#!/usr/bin/env bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

log_level=${log_level:=info}
assist_dir=${assist_dir:=$this_script_dir/assist}
source $assist_dir/global.sh

################################################################################
#                                                                          80col
print_help()
{
    cat <<EOF
    useage:
    backup linux wf or macos: $0 --backup
    backup book to seagate:   $0 --xfs_to_seagate
    backup book to seagate:   $0 --exfat_to_seagate

    --backup              - backup linux wf or macos
    --xfs_to_seagate      - backup book to seagate ; xfs   of linux
    --exfat_to_seagate    - backup exfat to seagate; exfat of windows

    --log_level=<debug>   - control pint log on streen,trace|debug|info|warn|error|critical

    --help  - prints help screen, script will exit after printing


EOF
}

init_var()
{
    local book_dir='vi@192.168.31.81:~/book'  #server s6
    local book_dir='vi@192.168.31.8:~/book'   #server wf
    local book_dir='vi@10.8.8.8:~/book'   #server wf

    if [ `uname` = "Linux" ] ; then     #linux
        path_all=( ${path_all[@]}
        #index even        odd
       "-q --delete" ~/run/ $book_dir/linux/run
       -qc ~/git/ssh/       $book_dir/linux/git/ssh
       -qc ~/git/hdocs/     $book_dir/linux/git/hdocs
       -qc ~/git/fip/       $book_dir/linux/git/fip
       -qc ~/git/wf3000/    $book_dir/linux/git/wf3000
       -qc ~/git/wf_update/ $book_dir/linux/git/wf_update
       -qc ~/git/cfg/       $book_dir/linux/git/cfg
       "-qc --delete" ~/.git_dir/      $book_dir/linux/git_dir)

    elif [ `uname` = "Darwin" ] ; then  #darwin
        path_all=( ${path_all[@]}
        #index even        odd
       "-q --delete -c" ~/git/com/       $book_dir/mac/git/com
       -qz ~/Pictures/                   $book_dir/mac/Pictures
       -q ~/Documents/                   $book_dir/mac/Documents
       #-q ~/git/ssh/      $book_dir/mac/git/ssh
       "-q --delete -c" ~/git/fip/       $book_dir/mac/git/fip
       "-q --delete -c" ~/git/wf3000/    $book_dir/mac/git/wf3000
       "-q --delete -c" ~/git/wf_update/ $book_dir/mac/git/wf_update
       "-qc --delete"   ~/git/cfg/       $book_dir/mac/git/cfg
       #"-qc "   ~/git/archive/   $book_dir/mac/git/archive
       "-qc --delete"   ~/git/chain/     $book_dir/mac/git/chain
       #-q               ~/git/tarball/   $book_dir/mac/git/tarball
       -qz ~/Music/        $book_dir/mac/Music
       -qz ~/Downloads/     $book_dir/mac/Downloads
       "-q -zc --delete" ~/git/chfs/     $book_dir/mac/git/chfs
       "-q -zc --delete" ~/git/cpp20/    $book_dir/mac/git/cpp20
       #"-q -zc --delete" ~/git/env_config/   $book_dir/mac/git/env_config
       "-q -zc --delete" ~/git/json_test/    $book_dir/mac/git/json_test
       "-q -zc --delete" ~/git/git_mgr/      $book_dir/mac/git/git_mgr
       #"-q -z"          "/Users/vi/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9"      $book_dir/mac/backup/wechat_backup/2.0b4.0.9
       #"-q" ~/git/      $book_dir/mac/git
    )
    fi
}

#rsync -a ~/git/ssh/ vi@192.168.31.8:$book_dir/git/ssh
#rsync -a ~/.git_dir/ vi@192.168.31.8:$book_dir/git_dir

check_valid()
{
    total_num=${#path_all[@]}
    let oe_flag=total_num%3
    if [ $oe_flag -ne 0 ] || [ $total_num -lt 3 ]; then #if is not even number
        vloge "path_all's number $total_num is not even, this is forbidden!"
        return 1
    else
        return 0
    fi
}

backup()
{
    vlogi "start backup"
    local src=""
    local dst=""
    local flag=0
    local parameter=""

    for((i = 0; i<${#path_all[@]}; ++i))
    do
        let flag=i%3
        [ $flag -eq 0 ] && parameter=${path_all[$i]}
        [ $flag -eq 1 ] && src=${path_all[$i]}
        [ $flag -eq 2 ] && dst=${path_all[$i]}

        if [ $flag -eq 2 ] ; then  #even number
            let idx_i=i/3
            let idx_t=total_num/3
            if [ -e "$src" ] ; then
                vlogi "[$idx_i/$idx_t] execute: rsync -a $parameter \"$src\" \"$dst\""
                rsync -a $parameter "$src" "$dst" 
            else
                #vlogw "execute: rsync -a \"$src\" \"$dst\" failed, src is not exist"
                vlogw "[$idx_i/$idx_t] src \"$src\" is not exist, skip backup it"
            fi
        fi
    done
}

xfs_to_seagate()
{
    local src=~/book/
    local dst=~/exfat_seagate/backup/book

    if [ ! -d "$src" ] ; then
        vloge "src dir is not exist, src:$src"
        return 1
    fi

    if [ ! -d "$dst" ] ; then
        vloge "dst dir is not exist, dst:$dst"
        return 1
    fi

    local parameter="-c --delete --partial --progress -h"
    vlogi "execute: rsync -a $parameter \"$src\" \"$dst\""
    rsync -aq $parameter "$src" "$dst" 
    return 0
}

exfat_to_seagate()
{
    local src=~/exfat/
    local dst=~/exfat_seagate/backup/exfat

    if [ ! -d "$src" ] ; then
        vloge "src dir is not exist, src:$src"
        return 1
    fi

    if [ ! -d "$dst" ] ; then
        vloge "dst dir is not exist, dst:$dst"
        return 1
    fi

    local parameter="-c --delete --partial --progress -h"
    vlogi "execute: rsync -a $parameter \"$src\" \"$dst\""
    rsync -aq $parameter "$src" "$dst" 
    reutrn 0
}

main()
{
    init_var
    check_valid || exit 1

    if [ -n "$help" ] || [ $_arg_num_ -eq 0 ]  ; then
        print_help
    elif [ -n "$xfs_to_seagate" ] ; then
        vlogi "backup"
        xfs_to_seagate
    elif [ -n "$exfat_to_seagate" ] ; then
        vloge "~/exfat dir already umount device, it should be empty now."
        return 1
        exfat_to_seagate
    elif [ -n "$backup" ] ; then
        backup
    fi
        return 0
}

main
exit 0

